package com.ba360.struts.action;

import com.ba360.portal.database.DBConnectionImpl;
import com.ba360.portal.database.dbo.User;
import com.ba360.portal.util.EmailSender;
import com.ba360.portal.util.Log;
import com.ba360.struts.form.ResponseForm;
import java.io.PrintStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class AdResponseAction extends Action
{
  public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    throws Exception
  {
    Properties prop;
    User user;
    int adId;
    ActionForward af = new ActionForward();
    af.setContextRelative(true);
    af.setRedirect(true);
    af.setName("Cancel");
    af.setPath("/portal/index.do");
    String action = request.getParameter("action");
    String cancel = request.getParameter("org.apache.struts.taglib.html.CANCEL");

    HttpSession session = request.getSession();
    ResponseForm rf = (ResponseForm)form;

    if ((cancel != null) && (cancel.equals("Cancel")))
    {
      return af;
    }
    if ((action != null) && (action.equals("Response"))) {
      prop = new Properties();
      user = (User)session.getAttribute("USER");
      adId = Integer.parseInt(request.getParameter("adId"));
      prop.setProperty("FullName", getAdUserName(adId));
      prop.setProperty("FromResponseAddress", rf.getRespEmail());
      prop.setProperty("ADResponse", rf.getRespMessage());
      prop.setProperty("AdTitle", request.getParameter("AdTitle"));
      prop.setProperty("ToEmailAddr", getAdUserEmailAddr(adId));
      prop.setProperty("ADID", adId+"");
      Log.init(null);
      Log.info("Statting response");
      try
      {
        EmailSender.sendEmail(EmailSender.TYPE_ADRESPONSE, prop);
        Log.info("Send response");
      }
      catch (Exception e) {
        System.out.println(e);
        Log.error(e.toString());
      }

      prop = new Properties();
      prop.setProperty("ADResponse", rf.getRespMessage());
      prop.setProperty("AdTitle", request.getParameter("AdTitle"));
      prop.setProperty("ToEmailAddr", user.getEmail());
      prop.setProperty("ADID", adId+"");
      try {
        EmailSender.sendEmail(EmailSender.TYPE_COPYADRESPONSE, prop);
      }
      catch (Exception e) {
        System.out.println(e);
      }

      return mapping.findForward("success"); }
    if ((action != null) && (action.equals("Complaint"))) {
      System.out.println("it comes inside complaint");
      System.out.println("The respondent email is :" + rf.getRespEmail());
      System.out.println("The title is" + request.getParameter("AdTitle"));
      Log.info("it comes inside complaint");
      Log.info("The respondent email is :" + rf.getRespEmail());
      Log.info("The title is" + request.getParameter("AdTitle"));

      prop = new Properties();
      user = (User)session.getAttribute("USER");
      adId = Integer.parseInt(request.getParameter("adId"));
      System.out.println("The ad is:" + adId);
      Log.info("The ad is:" + adId);
      prop.setProperty("FromComplaintAddress", rf.getRespEmail());
      prop.setProperty("subject", "Adv Complaint");
      prop.setProperty("ADComplaint", rf.getRespMessage());
      prop.setProperty("AdTitle", request.getParameter("AdTitle"));
      prop.setProperty("ToEmailAddr", "response@bangalore360.com");
      prop.setProperty("ADID", adId+"");
      Log.init(null);
      Log.info("Statting response");
      try
      {
        EmailSender.sendEmail(EmailSender.TYPE_COMPLAINT, prop);
        Log.info("Send complaint");
      } catch (Exception e) {
        System.out.println(e);
        Log.error(e.toString());
      }

      return mapping.findForward("success");
    }
    return mapping.findForward("failure");
  }

  public String getAdUserEmailAddr(int adId) throws Exception
  {
    DBConnectionImpl dbConn = new DBConnectionImpl();
    Connection con = dbConn.getConnection();
    Statement stmt = con.createStatement();

    ResultSet rs = stmt.executeQuery("select member_id,Contact_email from ads where ad_id=" + adId);
    rs.next();
    String email = rs.getString("Contact_email");
    int memId = rs.getInt("member_id");
    rs.close();
    if ((email == null) || (email.trim().length() == 0))
    {
      rs = stmt.executeQuery("select email from user_profile where userid=" + memId);
      rs.next();
      email = rs.getString("email");
      rs.close();
    }
    stmt.close();
    con.close();
    return email;
  }

  public String getAdUserName(int adId) throws Exception
  {
    DBConnectionImpl dbConn = new DBConnectionImpl();
    Connection con = dbConn.getConnection();
    Statement stmt = con.createStatement();

    ResultSet rs = stmt.executeQuery("select member_id,Contact_email from ads where ad_id=" + adId);
    rs.next();
    int memId = rs.getInt("member_id");
    rs.close();

    rs = stmt.executeQuery("select email from user_profile where userid=" + memId);
    rs.next();
    String fn = rs.getString("email");
    rs.close();

    stmt.close();
    con.close();
    return fn;
  }

  private void saveResponse(int adid, double userid, String adTitle, String response) throws Exception
  {
    DBConnectionImpl dbConn = new DBConnectionImpl();
    Connection con = dbConn.getConnection();
    Statement stmt = con.createStatement();

    stmt.execute("insert into responses (userid,adid,adTitle,message) values(" + 
      userid + "," + adid + ",'" + adTitle + "','" + response + "')");
  }
}